package bg.btanev.eightyfour.client.view;

import java.util.List;
import java.util.Set;

import bg.btanev.eightyfour.client.bundles.ProfileLogosImageBundle;
import bg.btanev.eightyfour.client.composite.ExpandableListElementComposite;
import bg.btanev.eightyfour.shared.dto.TagDto;
import bg.btanev.eightyfour.shared.dto.geo.GeoLocationDto;
import bg.btanev.eightyfour.shared.dto.profile.ProfileEventDto;
import bg.btanev.eightyfour.shared.dto.profile.feed.FeedMediaContentDimensionsDto;
import bg.btanev.eightyfour.shared.dto.profile.feed.FeedMediaContentDto;
import bg.btanev.eightyfour.shared.dto.profile.feed.FeedMediaContentMetaDto;
import bg.btanev.eightyfour.shared.dto.profile.feed.FeedProfileEventDto;

import com.google.gwt.dom.client.Style.FontStyle;
import com.google.gwt.dom.client.Style.FontWeight;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 *
 * @author B.Tanev
 *
 */
public class ProfileEventListElementView extends ExpandableListElementComposite {
	private final Label titleCaption;
	private final Label dateCaption;
	private final Image logoImage;

	private final VerticalPanel rootContentContainer;
	private ProfileEventDto profileEvent;
	private Grid titleContainer;
	private Grid contentInfoContainer;
	private FlowPanel tagsContainer;
	private Anchor externalLinkData;
	private DisclosurePanel mediaContentPanel;
	private VerticalPanel mediaContentContainer;
	private DisclosurePanel contentPanel;
	private HTML contentDataContainer;

	private boolean contentPanelCreated = false;

	public ProfileEventListElementView() {
		titleContainer = new Grid(1, 3);
		getHeaderPanel().add(titleContainer);
		titleContainer.setCellPadding(0);
		titleContainer.setCellSpacing(0);
		titleContainer.setWidth("100%");

		logoImage = new Image();
		titleContainer.setWidget(0, 0, logoImage);

		titleCaption = new InlineHTML("Title");
		titleContainer.setWidget(0, 1, titleCaption);
		titleCaption.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		titleCaption.setWordWrap(true);
		titleCaption.setWidth("900px");
		titleCaption.getElement().getStyle().setFontWeight(FontWeight.BOLD);

		dateCaption = new Label("");
		titleContainer.setWidget(0, 2, dateCaption);
		dateCaption.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		dateCaption.setWidth("200px");
		dateCaption.setWordWrap(false);
		dateCaption.getElement().getStyle().setFontStyle(FontStyle.ITALIC);

		titleContainer.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
		titleContainer.getCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_MIDDLE);
		titleContainer.getCellFormatter().setWidth(0, 0, "30px");

		titleContainer.getCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_MIDDLE);
		titleContainer.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_LEFT);

		titleContainer.getCellFormatter().setVerticalAlignment(0, 2, HasVerticalAlignment.ALIGN_MIDDLE);
		titleContainer.getCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_RIGHT);

		rootContentContainer = new VerticalPanel();
		getDataPanel().add(rootContentContainer);
		rootContentContainer.setSpacing(4);
		rootContentContainer.setWidth("100%");

		getOpenHandlers().addOpenHandler(new OpenHandler<DisclosurePanel>() {
			@Override
			public void onOpen(OpenEvent<DisclosurePanel> event) {
				if (!contentPanelCreated) {
					createContentPanelElements();
					updateContent(profileEvent);

					contentPanelCreated = true;
				}
			}
		});
	}

	private void createContentPanelElements() {
		rootContentContainer.clear();

		final DecoratorPanel decPanel = new DecoratorPanel();
		decPanel.setWidth("800px");
		rootContentContainer.add(decPanel);

		contentInfoContainer = new Grid(2, 2);
		contentInfoContainer.setCellPadding(5);
		contentInfoContainer.setWidth("100%");
		decPanel.add(contentInfoContainer);

		final Label externalLinkCaption = new Label("External Link:");
		externalLinkCaption.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		externalLinkCaption.setWidth("100px");
		contentInfoContainer.setWidget(0, 0, externalLinkCaption);
		contentInfoContainer.getCellFormatter().setWidth(0, 0, "100px");

		externalLinkData = new Anchor();
		contentInfoContainer.setWidget(0, 1, externalLinkData);
		externalLinkData.setWidth("100%");

		final Label tagsCaption = new Label("Tags:");
		tagsCaption.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		tagsCaption.setWidth("100px");
		contentInfoContainer.setWidget(1, 0, tagsCaption);
		contentInfoContainer.getCellFormatter().setWidth(1, 0, "100px");

		tagsContainer = new FlowPanel();
		tagsContainer.setWidth("100%");
		contentInfoContainer.setWidget(1, 1, tagsContainer);
		contentInfoContainer.getCellFormatter().setWordWrap(1, 1, true);

		contentInfoContainer.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		contentInfoContainer.getCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_MIDDLE);
		contentInfoContainer.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_LEFT);
		contentInfoContainer.getCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_MIDDLE);

		contentInfoContainer.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		contentInfoContainer.getCellFormatter().setVerticalAlignment(1, 0, HasVerticalAlignment.ALIGN_MIDDLE);
		contentInfoContainer.getCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_LEFT);
		contentInfoContainer.getCellFormatter().setVerticalAlignment(1, 1, HasVerticalAlignment.ALIGN_MIDDLE);

		mediaContentPanel = new DisclosurePanel("Media Content");
		mediaContentPanel.setOpen(false);
		mediaContentPanel.setAnimationEnabled(true);
		mediaContentPanel.setWidth("100%");
		rootContentContainer.add(mediaContentPanel);

		mediaContentContainer = new VerticalPanel();
		mediaContentContainer.setSpacing(2);
		mediaContentContainer.setWidth("100%");
		mediaContentPanel.setContent(mediaContentContainer);

		contentPanel = new DisclosurePanel("Content");
		contentPanel.setOpen(true);
		contentPanel.setWidth("100%");
		rootContentContainer.add(contentPanel);

		contentDataContainer = new HTML("No data...", true);
		contentPanel.setContent(contentDataContainer);
//		contentDataContainer.setWidth("100%");
	}

	public ProfileEventDto getProfileEvent() {
		return profileEvent;
	}

	public void setProfileEvent(ProfileEventDto profileEvent) {
		this.profileEvent = profileEvent;

		if (this.profileEvent == null) {
			return;
		}

		updateHeader(profileEvent);
		// updateContent(profileEvent);
	}

	private void updateContent(ProfileEventDto profileEvent) {
		externalLinkData.setHref("");
		externalLinkData.setText("");

		if (profileEvent instanceof FeedProfileEventDto) {
			String link = ((FeedProfileEventDto) profileEvent).getLink();

			externalLinkData.setHref(link);
			externalLinkData.setText(link);
		}

		tagsContainer.clear();
		Set<TagDto> tags = profileEvent.getTags();
		if (tags != null && tags.size() > 0) {
			StringBuilder sb = new StringBuilder();
			for (TagDto tag : tags) {
				sb.append(tag.getId());
				sb.append(", ");
			}

			Label tagsCaption = new Label(sb.toString());
			tagsContainer.add(tagsCaption);
		} else {
			if (contentInfoContainer.getRowCount() > 1) {
				contentInfoContainer.removeRow(1);
			}
		}

		GeoLocationDto geoLocation = profileEvent.getGeoLocation();
		if (geoLocation != null) {
			double lon = geoLocation.getLongitude();
			double lat = geoLocation.getLatitude();

			int rowCount = contentInfoContainer.getRowCount();
			int newRow = contentInfoContainer.insertRow(rowCount - 1);

			Label locationCaption = new Label("Location:");
			locationCaption.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
			locationCaption.setWidth("100px");
			contentInfoContainer.setWidget(newRow, 0, locationCaption);
			contentInfoContainer.getCellFormatter().setWidth(newRow, 0, "100px");

			String caption = "Longitude:" + lon + " Latitude:" + lat;
			String url = "http://maps.google.com/maps?q=" + lat + "+" + lon;
			Anchor mapLink = new Anchor(caption, url);
			mapLink.setWidth("100%");

			contentInfoContainer.setWidget(newRow, 1, mapLink);

			contentInfoContainer.getCellFormatter().setHorizontalAlignment(newRow, 0,
					HasHorizontalAlignment.ALIGN_RIGHT);
			contentInfoContainer.getCellFormatter().setVerticalAlignment(newRow, 0, HasVerticalAlignment.ALIGN_MIDDLE);
			contentInfoContainer.getCellFormatter()
					.setHorizontalAlignment(newRow, 1, HasHorizontalAlignment.ALIGN_LEFT);
			contentInfoContainer.getCellFormatter().setVerticalAlignment(newRow, 1, HasVerticalAlignment.ALIGN_MIDDLE);
		}

		if (profileEvent instanceof FeedProfileEventDto) {
			FeedProfileEventDto feedPe = (FeedProfileEventDto) profileEvent;
			List<FeedMediaContentDto> mediaContent = feedPe.getMediaContent();
			if (mediaContent != null && mediaContent.size() > 0) {
				mediaContentPanel.setVisible(true);
				mediaContentContainer.clear();

				for (FeedMediaContentDto mc : mediaContent) {
					FeedMediaContentMetaDto mcMeta = mc.getMeta();
					if (mcMeta != null) {
						String title = mcMeta.getTitle();
						if (title != null && title.length() > 0) {
							Label metaTitleCaption = new Label(title);
							metaTitleCaption.setWidth("100%");
							metaTitleCaption.setWordWrap(true);
							metaTitleCaption.getElement().getStyle().setFontWeight(FontWeight.BOLD);
							mediaContentContainer.add(metaTitleCaption);
						}

						String desc = mcMeta.getDescription();
						if (desc != null && desc.length() > 0) {
							Label metaDescCaption = new Label(desc);
							metaDescCaption.setWidth("100%");
							metaDescCaption.setWordWrap(true);
							metaDescCaption.getElement().getStyle().setFontStyle(FontStyle.ITALIC);
							mediaContentContainer.add(metaDescCaption);
						}
					}

					String mcUrl = mc.getUrl();
					if (mcUrl != null && mcUrl.length() > 0) {
						Frame frame = new Frame(mcUrl);

						FeedMediaContentDimensionsDto dim = mc.getDimensions();
						if (dim != null) {
							frame.setPixelSize(dim.getWidth(), dim.getHeight());
						}

						mediaContentContainer.add(frame);
					}
				}
			} else {
				mediaContentPanel.setVisible(false);
			}
		}

		if (profileEvent.getDescription() != null) {
			contentDataContainer.setHTML("<p>" + profileEvent.getDescription() + "</p>");
		} else {
			contentPanel.setVisible(false);
		}
	}

	private void updateHeader(ProfileEventDto profileEvent) {
		titleCaption.setText("");
		dateCaption.setText("");

		ProfileLogosImageBundle logos = ProfileLogosImageBundle.INSTANCE;
		switch (profileEvent.getProfile().getType()) {
			case BUZZ:
				logoImage.setResource(logos.buzzLogoSmall());
				break;

			case FLICKR:
				logoImage.setResource(logos.flickrLogoSmall());
				break;

			case GOOGLE_READER:
				logoImage.setResource(logos.gReaderLogoSmall());
				break;

			case FEED:
				logoImage.setResource(logos.feedLogoSmall());
				break;

			case PICASSA:
				logoImage.setResource(logos.picassaLogoSmall());
				break;

			case TWITTER:
				logoImage.setResource(logos.twitterLogoSmall());
				break;

			case YOU_TUBE:
				logoImage.setResource(logos.youTubeLogoSmall());
				break;

			default:
				logoImage.setResource(logos.feedLogoSmall());
				break;
		}

		titleCaption.setText(this.profileEvent.getTitle());
		DateTimeFormat formatter = DateTimeFormat.getMediumDateTimeFormat();
		dateCaption.setText(formatter.format(this.profileEvent.getDate()));
	}
}
